﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using FakeItEasy;
using System.Transactions.Abstractions;
using System.Transactions;

namespace Legend.Transactions.Tests
{
    [TestFixture]
    public class TransactionParticipantTests
    {
        private ITransactionParticipant participant;
        private ITransactionManager transactionManager;
        private ITransaction transaction;

        [SetUp]
        public void SetUp()
        {
            this.participant = A.Fake<ITransactionParticipant>();
            this.transactionManager = A.Fake<ITransactionManager>();
            this.transaction = A.Fake<ITransaction>();

            A.CallTo(() => this.transactionManager.CurrentTransaction).Returns(this.transaction);
        }

        [Test, ExpectedException(typeof(ArgumentNullException))]
        public void EnlistVolatile_throws_when_transactionManager_is_null()
        {
            TransactionParticipant.EnlistVolatile(A.Fake<ITransactionParticipant>(), null);
        }

        [Test, ExpectedException(typeof(ArgumentNullException))]
        public void EnlistVolatile_throws_when_transactionParticipant_is_null()
        {
            TransactionParticipant.EnlistVolatile(null, A.Fake<ITransactionManager>());
        }

        [Test]
        public void EnlistVolatile_enlists_transactionParticipant_in_specified_transactionManager()
        {
            this.participant.EnlistVolatile(this.transactionManager);
        }

        [Test]
        public void EnlistVolatile_returns_enlistment_returned_from_EnlistVolatile_on_transaction()
        {
            var enlistment = A.Fake<IEnlistment>();

            A.CallTo(() => this.transaction.EnlistVolatile(this.participant, EnlistmentOptions.None)).Returns(enlistment);

            var returned = this.participant.EnlistVolatile(this.transactionManager);

            Assert.That(returned, Is.SameAs(enlistment));
        }
    }
}
